#include "H/MAIN.H"
#include <stdio.h>

#if __C51
uint8_t 	F_UART0TI = 0;
#endif

uint16_t TH_KeyValue[3] = {0};					
/**********************************************************************************************************************
默认功能如下：
通道使能： 	20路(TH1-TH20) 
阈值设置模式： 自适应
低功耗使能： 	 使能
LED单控使能模式: 触摸使能通道为 COM 扫描输出单控，触摸未使能通道为 GPIO 单控
超长按键复位： 按键有效超过 16S，所有使能的触摸通道复位
休眠等待时间： 无触摸休眠等待时间 4S
LED复用及状态: TH1-TH20与LED复用，上电时每排LED依次亮500ms后全亮500ms后熄灭，之后关闭LED单控模式，复用的LED跟随触摸按键 \
触摸按下时点亮，松开时熄灭，数码管亮对应的编号数字2S，小数点及D21蓝色指示灯按下时亮，松开时熄灭, \
I2C在休眠等待时间内一直有I2C通讯时，不会进休眠。
键值读取地址为: 0x22(TCH8~TCH1) 0x23(TCH16~TCH9) 0x24(TCH20~TCH17)  
注意: 1.除控制LED的LEDC1-LEDC3寄存器写入立即生效外，其余任何寄存器的每一次修改，都必须写一次[CTRL1,0] = 1;修改才会生效。\
2.触摸按键按下和松开时都会产生INT中断，主机只要收到INT下降沿中断后读取键值，其余时间不必频繁读取，以识别按键是按下还是松开。
3.当使用自适应阈值模式时，如不灵敏，则修改ATRH_L宏，越小越灵敏，最小为20，反之过于灵敏则调大，当芯片休眠后触摸无法唤醒\
则修改LP_WAKETHRESHOLD宏，越小越容易唤醒，最小为10。如发现容易自唤醒，可适当调大此值，如依然存在自唤醒现象。\
请将CTRL2[0] = 1; 将LDO使能。则可解决自唤醒现象(功耗增加3uA)。
4.当主控有接RST复位引脚时，主控非必要时尽量不主动发复位信号复位PT2020。PT2020复位后，CTRL1默认值为0xD4,I2C读取为0xD4
***********************************************************************************************************************/

//当不采用自适应即[CTRL1,07] == 0时，需要手动设置触摸阈值时修改下表对应触摸通道，且USE_MANUAL_THRESHOLD = 1，自适应时USE_MANUAL_THRESHOLD = 0
#if (TOUCH_CTRL1_SET&0x80)
#define   USE_MANUAL_THRESHOLD				0
#else
#define 	USE_MANUAL_THRESHOLD				1
#endif
uint8_t code  Manual_threshold[20] = {
30,					//TH1
30,					//TH2
30,					//TH3
30,					//TH4
30,					//TH5
30,					//TH6	
30,					//TH7
30,					//TH8
30,					//TH9
30,					//TH10
30,					//TH11
30,					//TH12
30,					//TH13
30,					//TH14
30,					//TH15
30,					//TH16
30,					//TH17
30,					//TH18
30,					//TH19
30};				//TH20


/******************************************************************************
*函数名称：PT2020_INIT
*函数功能：PT2020初始化流程
*入口参数：
*出口参数：1 or -1
*******************************************************************************/
int8_t PT2020_INIT(void)
{
		uint8_t i = 10;
		delay_ms(50);																			//等待PT2020初始化完成
		//发生I2C写错误，继续写，最多10次
	  while(i--)
		{
				if(PT2020_Touch_Init())												//I2c初始化PT2020
				{
						if(PT2020_Readback_Config_check())				//参数回读确认
						{
								i=0;		
								return 1;															
						}
				}
		}
			return -1;
}

/******************************************************************************
*函数名称：PT2020_Touch_Channel_Config
*函数功能：配置触摸通道,如发生写入失败，重写，最多5次
*入口参数：channel	-->TH1-TH20
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*******************************************************************************/
int8_t  PT2020_Touch_Channel_Config(uint32_t channel)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t =  PT2020_I2C_WriteData(PT2020_THEN1,(uint8_t)channel&0xFF);
			if(err_t == false)
				continue;
			err_t = PT2020_I2C_WriteData(PT2020_THEN2,(uint8_t)(channel>>8)&0xFF);
			if(err_t == false)
				continue;
			err_t = PT2020_I2C_WriteData(PT2020_THEN3,(uint8_t)(channel>>16)&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Adaptive_ATRH_L_Config
*函数功能：自适应模式阈值配置，[CTRL1,07] = 1时有效，如发生写入失败，重写，最多5次
*入口参数：ATR	-->范围[10~1024]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t  PT2020_Touch_Adaptive_ATRH_L_Config(uint16_t ATR)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_ATRL,(uint8_t)ATR&0xFF);
			if(err_t == false)
				continue;
			err_t = PT2020_I2C_WriteData(PT2020_ATRH,(uint8_t)(ATR>>8)&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Threshold_Manual_Set_Config
*函数功能：手动设置触摸通道的触摸阈值。如发生写入失败，重写，最多5次
*入口参数：channel-->触摸通道[TH1-TH20] THSx	-->范围[20~250]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t 	PT2020_Touch_Threshold_Manual_Set_Config(uint8_t channel,uint8_t THSx)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(channel,THSx&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_LED_Reuse_Confing
*函数功能：配置触摸通道复用LED。如发生写入失败，重写，最多5次
*入口参数：led-->与开启的触摸通道匹配
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t	PT2020_Touch_LED_Reuse_Confing(uint32_t led)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_LEDC1,(uint8_t)led&0xFF);
			if(err_t == false)
				continue;
			err_t = PT2020_I2C_WriteData(PT2020_LEDC2,(uint8_t)(led>>8)&0xFF);
			if(err_t == false)
				continue;
			err_t = PT2020_I2C_WriteData(PT2020_LEDC3,(uint8_t)(led>>16)&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Longpress_Time_Reset_Config
*函数功能：配置触摸系统复位的长按时间。如发生写入失败，重写，最多5次
*入口参数：time-->范围[1~60]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_Longpress_Time_Reset_Config(uint8_t time)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_LKTIME,time&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Into_LowPower_WaitTime_Config
*函数功能：配置进入低功耗的等待时间。如发生写入失败，重写，最多5次
*入口参数：time-->范围[1~8]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_Into_LowPower_WaitTime_Config(uint8_t time)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_LPTIME,time&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Into_LowPower_WaitTime_Config
*函数功能：配置触摸唤醒低功耗的触摸阈值。如发生写入失败，重写，最多5次
*入口参数：wake-->范围[2~128]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_LowPower_Wakeupthreshold_Config(uint8_t wake)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_LPWK,wake&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Key_Debounce_times_Config
*函数功能：配置触摸按键消抖次数。如发生写入失败，重写，最多5次
*入口参数：debounce-->范围[1~10]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_Key_Debounce_times_Config(uint8_t debounce)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_KEYIF,debounce&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Key_Debounce_times_Config
*函数功能：配置基值增加时更新速度。如发生写入失败，重写，最多5次
*入口参数：baseup-->范围[1~250]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_BaseUp_Updatespeed_Config(uint8_t baseup)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_BUPH,baseup&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_BaseDown_Updatespeed_Config
*函数功能：配置基值减少时更新速度。如发生写入失败，重写，最多5次
*入口参数：basedown-->范围[1~250]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_BaseDown_Updatespeed_Config(uint8_t basedown)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_BUPL,basedown&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;

}
/********************************************************************************
*函数名称：PT2020_Touch_CMOD_ChargingTime_Config
*函数功能：配置CMOD电容充电时间。如发生写入失败，重写，最多5次
*入口参数：time-->范围[16~250]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_CMOD_ChargingTime_Config(uint8_t time)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_CMCT,time&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Lowpowerscan_Intervaltime_config
*函数功能：配置低功耗<--->触摸扫描的间隔时间。如发生写入失败，重写，最多5次
*入口参数：time-->范围[2~250]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_Lowpowerscan_Intervaltime_config(uint8_t time)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_LPST,time&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}

/********************************************************************************
*函数名称：PT2020_Touch_Set_ReuseLed_Output_config
*函数功能：配置触摸复用的LED点亮或者熄灭。配置后，写[CTRL2.3] = 1生效如发生写入失败，重写，最多5次
*入口参数：LEDSET-->LED的目标地址0x1F-0x21; LED -->按BIT 亮灭LED
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_Set_ReuseLed_Output_config(uint8_t LEDSET,uint8_t LED)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(LEDSET,LED);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}

/********************************************************************************
*函数名称：PT2020_Touch_Set_LedScan_Time_config
*函数功能：配置TOUCH复用的LED 扫描时间间隔，设置越小越亮，最小值为5，最大值为50
*入口参数：LEDSET-->LED的目标地址0x1F-0x21; LED -->按BIT 亮灭LED
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_Set_LedScan_Time_config(uint8_t time)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_LEDSET,time);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_CTRL1_Enable_Config
*函数功能：配置CTRL1使能某些功能，具体详见手册。如发生写入失败，重写，最多5次
*入口参数：enable-->范围[bit0~bit7]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_CTRL1_Enable_Config(uint8_t enable)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_CTRL1,enable&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Touch_CTRL1_Enable_Config
*函数功能：配置CTRL2使能某些功能，具体详见手册。如发生写入失败，重写，最多5次
*入口参数：enable-->范围[bit0~bit2]
*出口参数：err_t--> =1 写入成功; = -1 写入失败
*********************************************************************************/
int8_t PT2020_Touch_CTRL2_Enable_Config(uint8_t enable)
{
		uint8_t i = 5;
		int8_t err_t = -1;
	  do
		{
			err_t = PT2020_I2C_WriteData(PT2020_CTRL2,enable&0xFF);
			if(err_t == false)
				continue;
			if(err_t == true)
				break;
		}while(i--);
		if(err_t == true)
			return 1;
		else 
			return -1;
}
/********************************************************************************
*函数名称：PT2020_Get_Touch_KeyValue  如需单字节读取键值，请在收到INT下降沿中断后，调用此程序
*函数功能：读取触摸按键,在INT中断信号时读取。如发生写入失败，重写，最多5次		\
此函数如在中断中调用，则禁止在此函数中使用printf或者自定义的打印函数
*入口参数：
*出口参数：R_READ_DATA -->返回物理触摸通道，即0x00000001-->TH1,0x00000010-->TH5
*********************************************************************************/
int32_t PT2020_Get_Touch_KeyValue(void)
{
//		uint8_t key_l,key_m,key_h;
		uint16_t TH_CHTL1;
		uint8_t j = 5;
		uint32_t i = 0;
		int32_t R_READ_DATA = 0;
		while(j--)
		{
				TH_CHTL1 = PT2020_I2C_ReadData(PT2020_CTRL1); 	 								//读CTRL1,如发现与写入配置不一致时，连续5次都不一致，则重新初始化PT2020
				if(TH_CHTL1 == 0x100)
						continue;
				if((TH_CHTL1&0x01) != 0x01)
				{
#ifdef	DEBUG
				printf(" PT2020 abnormal reset and needs reinit!\r\n");
#endif
						PT2020_INIT();																   						//重新初始化(重新跑初始化流程)
					  return -1;																	     						//因为读按键时存在重新初始化，则返回主循环等待下次INT中断
				}
				break;			
		}	
		if(TH_CHTL1 == 0x100|| j == 0)
		{
#ifdef	DEBUG
				printf(" I2C abnormality!\r\n");
				return -1;
#endif		
		}
		TH_KeyValue[0] = PT2020_I2C_ReadData(PT2020_KEYL);       					//读通道1-8的u8_data
		TH_KeyValue[1] = PT2020_I2C_ReadData(PT2020_KEYM);       					//读通道9-16的u8_data
		TH_KeyValue[2] = PT2020_I2C_ReadData(PT2020_KEYH);       					//读通道17-20的u8_data
		R_READ_DATA = ((uint32_t)TH_KeyValue[2]<<16) + ((uint32_t)TH_KeyValue[1]<<8) + TH_KeyValue[0];
		return R_READ_DATA;
}

/********************************************************************************
*函数名称：PT2020_Get_Touch_KeyValue	如需连续读取键值，请在收到INT信号后，调用此程序
*函数功能：读取触摸按键,在INT中断信号时读取,I2C模式的连续读	\
此函数如在中断中调用，则禁止在此函数中使用printf或者自定义的打印函数
*入口参数：
*出口参数：将键值读入到TH_KeyValue数组中
*********************************************************************************/
int32_t PT2020_Read_Touch_KeyValue(void)
{
		uint16_t TH_CHTL1;
		uint8_t i = 5;
		while(i--)
		{
				TH_CHTL1 = PT2020_I2C_ReadData(PT2020_CTRL1); 	      								//读CTRL1,如发现与写入配置不一致时，连续5次都不一致，则重新初始化PT2020
				if(TH_CHTL1 == 0x100)
						continue;
				if((TH_CHTL1&0x01) != 0x01)
				{
#ifdef	DEBUG
				printf(" PT2020 abnormal reset and needs reinit!\r\n");
#endif
						PT2020_INIT();																   									//重新初始化(重新跑初始化流程)
					  return -1;																	    								  //因为读按键时存在重新初始化，则返回主循环等待下次INT中断
				}
				break;
					
		}
		if(TH_CHTL1 == 0x100 || i== 0)
		{
#ifdef	DEBUG
				printf(" I2C abnormality!\r\n");
				return -1;
#endif		
		}
	  PT2020_I2C_ContReadData(PT2020_KEYL,TH_KeyValue,sizeof(TH_KeyValue)/2);
		Touch_Keyvalue = (uint32_t)TH_KeyValue[2]<<16 |(uint32_t)TH_KeyValue[1]<<8 | TH_KeyValue[0];
		return 1;
}
/********************************************************************************
*函数名称：PT2020_Readback_Config_check
*函数功能：回读写入的配置信息，校验是否写入成功
*入口参数：
*出口参数：1 or -1
*********************************************************************************/
int32_t PT2020_Readback_Config_check(void)
{
		uint8_t i;
		uint8_t CTRL1_Buf = 0;
		uint16_t ReadBack = 0;
		ReadBack = PT2020_I2C_ReadData(PT2020_CTRL1);
		if(ReadBack != TOUCH_CTRL1_SET)
		{
#ifdef	DEBUG
				printf(" CTRL1 is Read/write data mismatch!\r\n");
#endif	
				return -1;
			
		}
		CTRL1_Buf = ReadBack;																														//保存回读的CTRL1数据，用于后面是否需要回读校验LEDCx
		ReadBack = PT2020_I2C_ReadData(PT2020_CTRL2);
		if(ReadBack != TOUCH_CTRL2_SET)
		{
#ifdef	DEBUG
				printf(" CTRL2 is Read/write data mismatch!\r\n");
#endif		
				return -1;
		
		}
		ReadBack = PT2020_I2C_ReadData(PT2020_ATRH);
		if(ReadBack != ((ATRH_L>>8)&0xff))
		{
#ifdef	DEBUG
				printf(" ATRH is Read/write data mismatch!\r\n");
#endif				
				return -1;

		}
		ReadBack = PT2020_I2C_ReadData(PT2020_ATRL);
		if(ReadBack != (ATRH_L&0xff))
		{
#ifdef	DEBUG
				printf(" ATRL is Read/write data mismatch!\r\n");
#endif	
				return -1;
			
		}
		ReadBack = PT2020_I2C_ReadData(PT2020_THEN1);
		if(ReadBack != (TH_CHANNEL&0x000000ffL))
		{
#ifdef	DEBUG
				printf(" THEN1 is Read/write data mismatch!\r\n");
#endif		
				return -1;
		
		}
		ReadBack = PT2020_I2C_ReadData(PT2020_THEN2);
		if(ReadBack != ((TH_CHANNEL>>8)&0x000000ffL))
		{
#ifdef	DEBUG
				printf(" THEN2 is Read/write data mismatch!\r\n");
#endif			
				return -1;
	
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_THEN3);
		if(ReadBack != ((TH_CHANNEL>>16)&0x000000ffL))
		{
#ifdef	DEBUG
				printf(" THEN3 is written not OK!\r\n");
#endif		
				return -1;
		
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_THEN3);
		if(ReadBack != ((TH_CHANNEL>>16)&0x000000ffL))
		{
#ifdef	DEBUG
				printf(" THEN3 is Read/write data mismatch!\r\n");
#endif		
				return -1;
		
		}	
#if 	USE_MANUAL_THRESHOLD
		for(i=0;i<20;i++)
		{
				ReadBack = PT2020_I2C_ReadData(PT2020_THS1+i);
				if(ReadBack != Manual_threshold[i])
				{
#ifdef	DEBUG
						printf(" THEN%bd is Read/write data mismatch!\r\n",i);
#endif	
						return -1;
			
				}	
		}
#endif
			ReadBack = PT2020_I2C_ReadData(PT2020_LPWK);
		if(ReadBack != LP_WAKETHRESHOLD)
		{
#ifdef	DEBUG
				printf(" LPWK is Read/write data mismatch!\r\n");
#endif		
				return -1;
		
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_LKTIME);
		if(ReadBack != LONGPRESS_RESET_TIME)
		{
#ifdef	DEBUG
				printf(" LKTIME is Read/write data mismatch!\r\n");
#endif		
				return -1;
		
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_LPTIME);
		if(ReadBack != INTO_LOWPOER_WAIT_TIME)
		{
#ifdef	DEBUG
				printf(" LPTIME is Read/write data mismatch!\r\n");
#endif		
				return -1;
		
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_KEYIF);
		if(ReadBack != DEBOUNCE_TIMES)
		{
#ifdef	DEBUG
				printf(" KEYIF is Read/write data mismatch!\r\n");
#endif	
				return -1;
			
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_BUPH);
		if(ReadBack != BASEUP_UPDATESPEED)
		{
#ifdef	DEBUG
				printf(" BUPH is Read/write data mismatch!\r\n");
#endif	
				return -1;
			
		}		
		ReadBack = PT2020_I2C_ReadData(PT2020_BUPL);
		if(ReadBack != BASEDOWN_UPDATESPEED)
		{
#ifdef	DEBUG
				printf(" BUPL is Read/write data mismatch!\r\n");
#endif		
				return -1;		
		}		
		ReadBack = PT2020_I2C_ReadData(PT2020_CMCT);
		if(ReadBack != CMOD_CHARGING_TIME)
		{
#ifdef	DEBUG
				printf(" CMCT is Read/write data mismatch!\r\n");
#endif	
				return -1;
			
		}	
		ReadBack = PT2020_I2C_ReadData(PT2020_LPST);
		if(ReadBack != LP_INTERVALTIME_SCAN)
		{
#ifdef	DEBUG
				printf(" LPST is Read/write data mismatch!\r\n");
#endif		
				return -1;
		}	
		if((CTRL1_Buf&0x10))																														//仅当CTRL1[4] = 1时，做LEDEN1-LEDEN3的回读校验
		{
				
			  ReadBack = PT2020_I2C_ReadData(PT2020_LEDC1);
				if(ReadBack != (TOUCH_LED_REUSE&0x000000ffL))
				{
#ifdef	DEBUG
						printf(" LEDC1 is Read/write data mismatch!\r\n");
#endif	
						return -1;
			
				}	
				ReadBack = PT2020_I2C_ReadData(PT2020_LEDC2);
				if(ReadBack != ((TOUCH_LED_REUSE>>8)&0x000000ffL))
				{
#ifdef	DEBUG
						printf(" LEDC2 is Read/write data mismatch!\r\n");
#endif	
						return -1;
			
				}	
				ReadBack = PT2020_I2C_ReadData(PT2020_LEDC3);
				if(ReadBack != ((TOUCH_LED_REUSE>>16)&0x000000ffL))
				{
#ifdef	DEBUG
						printf(" LEDC3 is Read/write data mismatch!\r\n");
#endif	
						return -1;
			
				}	
				ReadBack = PT2020_I2C_ReadData(PT2020_LEDSET);
				if(ReadBack != TOUCH_LED_SCAN_TIME)
				{
#ifdef	DEBUG
						printf(" LEDSET is Read/write data mismatch!\r\n");
#endif		
						return -1;
		
				}	
				
		}
		return 1;
}
/********************************************************************************
*函数名称：PT2020_Touch_Init
*函数功能：初始化PT2020,不需要配置的可屏蔽或设置成默认值			\
用户根据需要自行配置，初始化之后如修改配置，请先读出，仅修改需要改变的BIT位，然后再写回 \
用户可根据项目自由配置。
*入口参数：
*出口参数：
*********************************************************************************/
int8_t PT2020_Touch_Init(void)
{
		uint8_t i;
		int8_t err_t = 0;
		uint8_t config_offset = 0;
		uint32_t init_ok = 0;
		err_t = PT2020_Touch_Channel_Config(TH_CHANNEL);																//配置触摸通道
	if(err_t == -1)
	{
			config_offset++;
#ifdef	DEBUG
			printf(" The touch channel config failed!\r\n");
#endif
				}
		  err_t = PT2020_Touch_Adaptive_ATRH_L_Config(ATRH_L);													//自适应阈值配置，自适应阈值模式(CTRL1,07 = 1)有效
			if(err_t == -1)
			{
					config_offset++;
#ifdef	DEBUG
					printf(" The Adaptive threshold config failed!\r\n");
#endif
			}
#if 	USE_MANUAL_THRESHOLD
			for(i=0;i<20;i++)
			{
						//手动设置阈值时，不管触摸通道是否开启，都给相应通道设置默认触摸阈值
					err_t = PT2020_Touch_Threshold_Manual_Set_Config(PT2020_THS1+i,Manual_threshold[i]);
					if(err_t == -1)
					{
							config_offset ++;
#ifdef	DEBUG
							printf(" The TH[%bd] threshold Set is failed!\r\n",i);
#endif	
					}
				
			}
#endif 
			err_t = PT2020_Touch_LowPower_Wakeupthreshold_Config(LP_WAKETHRESHOLD);				//配置触摸唤醒阈值
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The LP wakeupthreshold config failed!\r\n");
#endif
			}
			err_t = PT2020_Touch_Longpress_Time_Reset_Config(LONGPRESS_RESET_TIME);				//配置复位所有触摸按键的长按时间，这里的复位是指复位触摸按键，防止触摸按键因介质改变导致锁死无法触摸的情况
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The Longpress time Reset config failed!\r\n");
#endif
			}		
			err_t = PT2020_Touch_Into_LowPower_WaitTime_Config(INTO_LOWPOER_WAIT_TIME);		//配置进入低功耗的等待时间

			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The Into lowpoer wait time config failed!\r\n");
#endif
			}	
			err_t = PT2020_Touch_Key_Debounce_times_Config(DEBOUNCE_TIMES);								//配置按键消抖次数
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The Key debounce times config failed!\r\n");
#endif
			}	
			err_t = PT2020_Touch_BaseUp_Updatespeed_Config(BASEUP_UPDATESPEED);						//配置基值增加的更新速度(当因外部环境变化(温湿度，介质等)导致基值向上时，当前基值更新到最新值的速度)
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH base up update speed config failed!\r\n");
#endif
			}	
			err_t = PT2020_Touch_BaseDown_Updatespeed_Config(BASEDOWN_UPDATESPEED);				//配置基值增加的更新速度(当因外部环境变化(温湿度，介质等)导致基值向下时，当前基值更新到最新值的速度)
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH base down update speed config failed!\r\n");
#endif
			}			
			err_t = PT2020_Touch_CMOD_ChargingTime_Config(CMOD_CHARGING_TIME);						//配置CMOD电容的充电时间(当CMOD电容>47nF时，默认的CMOD电容充电时间不足时，可修改此值)
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH CMOD charging time config failed!\r\n");
#endif
			}		
			err_t = PT2020_Touch_Lowpowerscan_Intervaltime_config(LP_INTERVALTIME_SCAN);	//配置低功耗模式时，触摸扫描间隔
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The lowpower scan interval time config failed!\r\n");
#endif
			}		
#if  (TOUCH_CTRL1_SET&0x10)
			err_t = PT2020_Touch_LED_Reuse_Confing(TOUCH_LED_REUSE);											//配置触摸引脚是否复用LED
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH LED reuse config failed!\r\n");
#endif
			}	
			err_t = PT2020_Touch_Set_LedScan_Time_config(TOUCH_LED_SCAN_TIME);						//配置触摸引脚复用的LED扫描时间，影响亮度，设置越小越亮，但同时触摸响应也会变慢，根据项目情况设置，设置范围见规格书描述
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH LED reuse config failed!\r\n");
#endif
			}	
#endif 
			err_t = PT2020_Touch_CTRL2_Enable_Config(TOUCH_CTRL2_SET);										//立即进入低功耗关闭，低功耗状态LDO关闭,LED单独控制使能
			if(err_t == -1)
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH CTRL2 config failed!\r\n");
#endif
			}	
			err_t = PT2020_Touch_CTRL1_Enable_Config(TOUCH_CTRL1_SET);										//任何配置发生了更改，都必须将[CTRL1,0]写1才能生效。默认复用的LED在触摸按下时,LED亮，松开LED熄灭
			if(err_t == -1)																				//
			{
					config_offset ++;
#ifdef	DEBUG
					printf(" The TH CTRL1 config failed!\r\n");
#endif
			}	
			if(config_offset == 0)																											  //当config_offset == 0时，说明初始化过程没有出现写入失败的情况，则输出打印初始化成功信息，否则打印失败信息
			{																																							//可根据需要，将此函数的类型修改为合适的类型，返回合适的值，配合系统进行传感器类芯片初始化信息的判断
					
#ifdef	DEBUG
					printf(" The PT2020 write successful!\r\n");
#endif
				  return 1;

			
			}
			else
			{
#ifdef	DEBUG
					printf(" The PT2020 write failed!\r\n");
#endif						
				return -1;
			}
}
#ifdef DEBUG

#if	__C51
/**
 * @brief  重定向putchar
 * @param  
 * @param  
 * @param 
 */
char putchar(char c)
{
	UAT0SBUF = c;
	while(F_UART0TI == 0)
	{
	}
	F_UART0TI = 0;
	return (c);

}
#else
// 根据实际芯片的函数接口重定向fputc函数到串口输出,即可使用printf打印
int fputc(int ch, FILE *f)
{
    // 等待串口发送完成
    while(0U == M0P_USART_GetStatus(USART_UNIT, UsartTxEmpty))
    {
    }
    
    // 发送字符
    M0P_USART_SendData(USART_UNIT, (uint8_t)ch);
    
    return ch;
}
#endif
#endif



